<template>

  <div class="ui attached pointing menu">
    <div class="ui container">
      <div class="right menu">
        <a href="/admin/type-input" class="active item">新增</a>
        <a href="/admin/type" class="teal  item">列表</a>
      </div>
    </div>
  </div>

  <!--中间内容-->
  <div  class="m-container-small m-padded-tb-big">
    <div class="ui container">
      <form class="ui form">
        <input type="hidden" name="id">
        <div class=" field">
          <div class="ui left labeled input">
            <label class="ui teal basic label">名称</label>
            <input type="text" placeholder="分类名称" v-model="tname" name="tname">
<!--            v-value="$route.query.name"-->
          </div>
        </div>

        <div class="ui error message"></div>
        <!--/*/
        <div class="ui negative message" th:if="${#fields.hasErrors('name')}"  >
          <i class="close icon"></i>
          <div class="header">验证失败</div>
          <p th:errors="*{name}">提交信息不符合规则</p>
        </div>
         /*/-->
        <div class="ui right aligned container">
          <button type="button" class="ui button" onclick="window.history.go(-1)" >返回</button>
          <button @click="push" class="ui teal submit button">提交</button>
<!--          @click="push"-->
        </div>

      </form>
    </div>
  </div>

  <br>
  <br>
  <br>
  <br>
  <br>
  <br>
  <br>
  <br>

</template>

<script>
import axios from "axios";

export default {
  data(){
    return {
      tname: '',
      id: -2
    }
  },
  watch: {
    '$route'(to, from) {
      this.$router.go(0);
    }
  },
  methods: {
    getType(){
      axios.get('http://localhost:7070/getType').then((res)=>{
        console.log(res.data);
        this.types = res.data;
      })
    },
    push(){
      let geturl = window.location.href;
      var strings = geturl.split('?');
      if (strings.length === 1){
        this.id = -1;
      }else {
        let getqyinfo = geturl.split('?')[1]
        let getqys = getqyinfo.split('&')
        this.id =  getqys[0].split('=')[1]
      };
      axios({
        method:"post",
        url:"http://localhost:7070/admin/type/post",
        data:{
          name: this.tname,
          id: this.id
        }
      }).then((res)=>{
        if (res.data === 1){
          sessionStorage.setItem('typeSuccess',"1");
          // this.$store.commit('setTypeSuccess');
        }else {
          console.log(res.data);
        }
      });
      // let self = this



      // console.log(sessionStorage.getItem('store'));

      this.$router.push({
        path: "/admin/type",
        query: {
          keywords: this.input,
          type: this.type,
          date:new Date().getTime()
        }
      })


      // self.$router.push({path: "/admin/type"});
      // let aboutRoute = self.$router.fullPath
      // if (aboutRoute === '/admin/type') {
      //   window.location.reload();
      // };
    }
  }
}
</script>

<style scoped>

</style>